package tw.com.jinwei.service;

import java.util.List;

import org.jdesktop.application.Application;
import org.jdesktop.swingx.auth.LoginService;

import tw.com.jinwei.db.User;
import tw.com.jinwei.db.UserDAO;

public class Login extends LoginService {

	private UserDAO userDao;
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

	private void doLogin(String name, String password) {
		User user = new User();
		user.setName(name);
		user.setPassword(password);

		List<User> userList = userDao.findByExample(user);
		if (userList.size() == 1) {
			this.user = userList.get(0);
		}
	}

	@Override
	public boolean authenticate(String name, char[] password, String server)
			throws Exception {
		this.doLogin(name, new String(password));
		if (user != null) {
			Application
					.launch(tw.com.jinwei.Application.class, new String[] {});
			return true;
		} else {
			return false;
		}
	}
}
